package com.junzhiit.nengji.app.web.util.tools;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class DIYTools {

	/**
	 * @param param(email=joe@qq.com&name=joe&sign=value)
	 * @return
	 */
	public static Map<String, String> splitForm(String args){
		Map<String, String> map = new HashMap<String, String>();
		String[] params = args.split("&");
		for(int i = 0,len = params.length; i < len; i++){
			String[] param = params[i].split("=");
			map.put(param[0], param[1]);
		}
		return map;
	}
	
	public static String URLDecoder(String value) throws UnsupportedEncodingException{
		return (value.indexOf("=")!=-1 && value.indexOf("+")!=-1 &&
				value.indexOf("/")!=-1)?value:URLDecoder.decode(value, "UTF-8");
	}
	
	public static String URLEncoder(String value) throws UnsupportedEncodingException{
		return (value.indexOf("=")!=-1 || value.indexOf("+")!=-1 ||
				value.indexOf("/")!=-1)?URLEncoder.encode(value, "UTF-8"):value;
	}
	
	public static void main(String[] args) {
		Map<String, String> map = splitForm("email=joezhou@uniyep.com&timestamp=1111&sign=AHXYSDN%#VJN");
		System.out.println(map.get("email"));
		System.out.println(map.get("timestamp"));
		System.out.println(map.get("sign"));
	}
}
